package plugins.line_tool_plugin;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import dymnd.image.Layer;
import dymnd.io.ConfigHashtable;
import dymnd.net.Packet;
import dymnd.plugin.PaintPlugin;
import dymnd.ui.SeekBar;
import dymnd.ui.ThemeConstants;



public class EPC extends PaintPlugin{
	
	Line2D.Double lastLine = null;
	ConfigHashtable config;
	
	//JLabel label = new JLabel("<html><body text=white><p>Size: </p></body></html>");
	
	
	double drawAlpha = 1;
	BufferedImage backbuf;
	
	SeekBar alphaSeekbar = new SeekBar(255);
	SeekBar sizeSeekbar = new SeekBar(200);
	int lastValidX = -1;
	int lastValidY = -1;
	
	public EPC()	{
		
		
		
		alphaSeekbar.setBackground(ThemeConstants.THEME_SEEKER_BACKGROUND_COLOR);
		alphaSeekbar.setSeekBarColor(ThemeConstants.THEME_SEEKER_BAR_COLOR);
		alphaSeekbar.setBorder(BorderFactory.createEtchedBorder(Color.lightGray, Color.darkGray));
		
	}
	
	public ImageIcon getIcon()	{
		return new ImageIcon(loadImage("plugins/line_tool_plugin/linetoolicon.png"));
	
	}
	public Container getToolbar(Dimension d)	{
		Container c = new Container();
		c.setLocation(0, 0);
		c.setSize(d.getSize());
		int labelWidth = 50;
		int componentHeight = 20;
		
		int sizeSeekbarWidth = 150;
		int opacitySeekbarWidth = 100;
		
		int x = 10;
		
		JLabel sizeLabel = new JLabel("Size: ");
		JLabel opacityLabel = new JLabel("Opacity: ");
		
		//size seekbar
		sizeLabel.setSize(30, componentHeight);
		sizeLabel.setLocation(x, (int)(d.getHeight()/2 - sizeLabel.getHeight()/2));
		sizeLabel.setForeground(ThemeConstants.THEME_PLUGIN_PANEL_TEXT_COLOR);
		sizeLabel.setFont(ThemeConstants.FONT_PLUGIN_TOOLBAR_COMPONENT);
		c.add(sizeLabel);
		
		sizeSeekbar.setSize(sizeSeekbarWidth, componentHeight);
		sizeSeekbar.setLocation(x += sizeLabel.getWidth(),(int)(d.getHeight()/2 - sizeSeekbar.getHeight()/2));
		sizeSeekbar.setPercent(1);
		c.add(sizeSeekbar);
		
		//opacity seekbar 
		opacityLabel.setSize(labelWidth, componentHeight);
		opacityLabel.setLocation(x += sizeSeekbar.getWidth() + 10, (int)(d.getHeight()/2 - sizeLabel.getHeight()/2));
		opacityLabel.setForeground(ThemeConstants.THEME_PLUGIN_PANEL_TEXT_COLOR);
		opacityLabel.setFont(ThemeConstants.FONT_PLUGIN_TOOLBAR_COMPONENT);
		c.add(opacityLabel);
		
		alphaSeekbar.setSize(opacitySeekbarWidth, componentHeight);
		alphaSeekbar.setLocation(x += opacityLabel.getWidth(),(int)(d.getHeight()/2 - alphaSeekbar.getHeight()/2));
		c.add(alphaSeekbar);
		
		
		return c;
	}
	
	
	boolean strokeStart = true;
	BasicStroke stroke;
	
	int alpha;
	int strokeStartX = 0;
	int strokeStartY = 0;
	public Packet onEvent(Packet p, Layer affectedLayer, Layer scratchLayer)	{
		scratchLayer.clear();
		int radius = (int)sizeSeekbar.getValue();
		if(strokeStart)	{
			
			p.lastX = p.x;
			p.lastY = p.y;
			
			strokeStartX = p.x;
			strokeStartY = p.y;
			
		}
		
		if(p.primaryColor.getAlpha() <= 0)
			 alpha = (int)alphaSeekbar.getValue();
		else 
			alpha = p.primaryColor.getAlpha();
			
		stroke = new BasicStroke((int)(sizeSeekbar.getValue()), BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
		
		if(p.mouseAction == Packet.MOUSE_ACTION_PRESSED)	{
			Graphics2D g = scratchLayer.createGraphics();
			g.setColor(new Color(200, 200, 200, 150));
			g.setStroke(stroke);
			g.draw(new Line2D.Double(p.x, p.y, strokeStartX, strokeStartY));
			g.dispose();
			scratchLayer.setModified(true);
		}	else	{
			Graphics2D g = affectedLayer.createGraphics();
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.setColor(p.primaryColor = new Color(p.primaryColor.getRed(), p.primaryColor.getGreen(), p.primaryColor.getBlue(), alpha));
			g.setStroke(stroke);
			g.draw(new Line2D.Double(p.x, p.y, strokeStartX, strokeStartY));
			g.dispose();
			affectedLayer.setModified(true);
			strokeStart = true;
			p.bounds = new Rectangle2D.Double(0, 0, affectedLayer.getWidth(), affectedLayer.getHeight());
			return p;
		}
		
		
		
		
		
		int x = Math.min(strokeStartX, p.x) - radius;
		int y = Math.min(strokeStartY, p.y) - radius;
		
		int width = Math.max(strokeStartX, p.x) + radius*2;
		int height = Math.max(strokeStartY, p.y) + radius*2;
	
		
		if(x < 0)
			x = 0;
		if(y < 0)
			y = 0;
		
		if(width <= 0)
			width = 1+radius*2;
		if(height <= 0)
			height = 1+radius*2;
		
		
		//does check for width+x and height+y
		if(x + width > affectedLayer.getWidth())	{
			width = width + (affectedLayer.getWidth() - (x+width));
		}
		
		if(y + height > affectedLayer.getHeight())	{
			height = height + (affectedLayer.getHeight() - (y+height));
		}
		
		if(width <= 0)	{
			x = affectedLayer.getWidth() - 2;
			width = 1;
		}
		
		if(height <= 0)	{
			y = affectedLayer.getHeight() - 2;
			height = 1;
		}
		
		
		
		strokeStart = false;
		p.bounds = new Rectangle2D.Double(x, y, width, height);
		return p;
	}
	
	
	
	
	public boolean usingDefaultCursor()	{
		return true;
	}
	
	public String getTooltip()	{
		return "Draws a line between two points";
	}
	
	public String getVersionString()	{
		return "line_tool.version.1.0";
	}
	
	
	public char getKeyChar()	{
		return 'l';
	}
	
	
	private Image loadImage(String file)	{
		return Toolkit.getDefaultToolkit().getImage(file);
	}
	
	public void onUnload()	{
		/*config.set("brushRadius",	"" + (Double)sizeSeekbar.getValue());
		config.set("alpha", "" + (Double)alphaSeekbar.getValue());*/
	}
	
}